#include <stdio.h>
#include <stdlib.h>

typedef struct u_data
{
    int id;
    int high;
} u_data;

u_data *my_realloc(u_data *D, int new_size, int old_size)
{   if (NULL == D )
{
    u_data *N = (u_data *)malloc(new_size);
    printf("为NULL");
    free(D);
    return N;
}

    if (0 == new_size)
    {   printf("大小为零");
        free(D);
        return D;
    }
    if (new_size < old_size)
    {   printf("小于");
        u_data *N = (u_data *)malloc(new_size);
        for (int i = 0; i < (new_size / sizeof(u_data)); i++)
        {
            N[i] = D[i];
        }
        free(D);
        return N;
    }
    u_data *N = (u_data *)malloc(new_size);
    for (int i = 0; i < (old_size / sizeof(u_data)); i++)
    {
        N[i] = D[i];
    }
    free(D);

    return N;
}

int main(int argc, char const *argv[])
{
    int new_size = 200 * sizeof(u_data);
    int old_size = 17000 * sizeof(u_data);
    u_data *D = (u_data *)malloc(old_size);
        for (size_t i = 0; i < 17000; i++)
    {   printf("j = %ld\n",i);
        printf("H = %d \tT = %d \n", D[i].id, D[i].high);
    }
    // printf("%ld\n",new_size / sizeof(u_data));
    // my_realloc(NULL, new_size, old_size);
    D = my_realloc(D, new_size, old_size);
    for (size_t i = 0; i < 17000; i++)
    {
        printf("你 = %ld\n", i);
        printf("H = %d \tT = %d \n", D[i].id, D[i].high);
    }
    return 0;
}
